//
//  HWGameRankController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/4.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import JXSegmentedView
class HWGameRankController: HWBaseViewController {
    
    var topImageView:UIImageView!
    var isHot = false
    var textTagCollectionView:TTGTextTagCollectionView!
    var listContainerView: JXSegmentedListContainerView!
    var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect.zero)
    var segmentedDataSource: JXSegmentedTitleDataSource!
    var httpManager = HWGameCenterHttpManager()
    var modelArr:[HWGameClassifyConfigModel] = []
    var titles:[String] = Array()
    var selectIndex = 0
    /// 刷新数据
    var refreshButton:UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        
        initFrame()
        
        loadData()
        
    }
    
}

extension HWGameRankController {
    private func initUI() -> Void {
        navigationView.isHidden = true
        
        topImageView = UIImageView()
        view.addSubview(topImageView)
        if isHot == true {
            topImageView.image = UIImage(named: "renyoubangxingtupian")
        }else {
            topImageView.image = UIImage(named: "xiamingetetenn")
        }
        
        textTagCollectionView = TTGTextTagCollectionView.init(frame: .zero)
        textTagCollectionView.delegate = self
        textTagCollectionView.alignment = TTGTagCollectionAlignment.left
        textTagCollectionView.scrollDirection = .horizontal
        textTagCollectionView.showsHorizontalScrollIndicator = false
        textTagCollectionView.manualCalculateHeight = true
        textTagCollectionView.horizontalSpacing = 10*widthScale
        textTagCollectionView.contentInset = UIEdgeInsets(top: 20*widthScale, left: 25*widthScale, bottom: 2, right: 2)
        view.addSubview(textTagCollectionView)
        
        listContainerView = JXSegmentedListContainerView(dataSource: self)
        listContainerView.scrollView.isScrollEnabled = false
        view.addSubview(listContainerView)
        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.titles = titles
        segmentedView.delegate = self
        segmentedView.dataSource = segmentedDataSource
        segmentedView.listContainer = listContainerView
        
        refreshButton = UIButton(type: .custom)
        refreshButton.frame = CGRect(x: 0, y: 0, width:100*widthScale, height: 40*widthScale)
        refreshButton.setTitle("点击重试", for: .normal)
        refreshButton.setTitleColor(.white, for: .normal)
        refreshButton.titleLabel?.font = .boldSystemFont(ofSize: 14*widthScale)
        refreshButton.layer.cornerRadius = 20*widthScale
        refreshButton.layer.masksToBounds = true
        refreshButton.addTarget(self, action: #selector(refreshButtonClick(_:)), for: .touchUpInside)
        UIColor.gradientColor(view: refreshButton, colors: [UIColor.colorWithHexString(hexString: "#FFB60A").cgColor,UIColor.colorWithHexString(hexString: "#F66A05").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
        view.addSubview(refreshButton)
        refreshButton.isHidden = true
    }
    
    private func initFrame() -> Void {
        topImageView.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.top.equalTo(9*widthScale)
            make.right.equalTo(-20*widthScale)
            make.height.equalTo(94*widthScale)
        }
        
        textTagCollectionView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(topImageView.snp.bottom)
            make.height.equalTo(40*widthScale)
        }
        listContainerView.snp.makeConstraints { make in
            make.top.equalTo(textTagCollectionView.snp.bottom).offset(5*widthScale)
            make.left.right.bottom.equalToSuperview()
        }
        
        refreshButton.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(100*widthScale)
            make.height.equalTo(40*widthScale)
        }
    }
}

extension HWGameRankController:JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}

extension HWGameRankController:JXSegmentedListContainerViewDataSource,JXSegmentedViewDelegate {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return segmentedDataSource.dataSource.count
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        
        let rankList = HWGameRankListController()
        rankList.mod = self.modelArr[index]
        if isHot {
            rankList.type = "1"
        }else {
            rankList.type = "2"
        }
        return rankList
    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        self.selectIndex = index
        for i in 0..<textTagCollectionView.allTags().count {
            textTagCollectionView.updateTag(at: UInt(i), selected: false)
        }
        textTagCollectionView.updateTag(at: UInt(self.selectIndex), selected: true)
    }
    
}

extension HWGameRankController:TTGTextTagCollectionViewDelegate{
    func textTagCollectionView(_ textTagCollectionView: TTGTextTagCollectionView!, didTap tag: TTGTextTag!, at index: UInt) {
        self.selectIndex = Int(index)
        for i in 0..<textTagCollectionView.allTags().count {
            textTagCollectionView.updateTag(at: UInt(i), selected: false)
        }
        textTagCollectionView.updateTag(at: UInt(self.selectIndex), selected: true)
        self.segmentedView.selectItemAt(index: self.selectIndex)
    }
}

extension HWGameRankController {
    func loadData() -> Void {
        httpManager.getGameRankListClassifyConfig { models in
            self.refreshButton.isHidden = true
            self.modelArr = models
            self.textTagCollectionView.removeAllTags()
            self.titles.removeAll()
            for (idx,mod) in models.enumerated() {
                self.titles.append(mod.name)
                let content = TTGTextTagStringContent.init(text: mod.name)
                content.textFont = UIFont.systemFont(ofSize: 10*widthScale)
                content.textColor = UIColor.colorWithHexString(hexString: "#888888")
                let normalStyle = TTGTextTagStyle.init()
                normalStyle.cornerRadius = 10*widthScale
                normalStyle.extraSpace = CGSize.init(width: 22*widthScale, height: 4*widthScale)
                normalStyle.backgroundColor = .white
                normalStyle.shadowColor = .white
                normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#CCCCCC")
                
                let selectContent = TTGTextTagStringContent.init(text: mod.name)
                selectContent.textFont = UIFont.systemFont(ofSize: 10*widthScale)
                selectContent.textColor = UIColor.colorWithHexString(hexString: "#F87706")
                let selectStyle = TTGTextTagStyle.init()
                selectStyle.cornerRadius = 10*widthScale
                selectStyle.extraSpace = CGSize.init(width: 22*widthScale, height: 4*widthScale)
                selectStyle.backgroundColor = .colorWithHexString(hexString: "#FFF1E4")
                selectStyle.shadowColor = .white
                selectStyle.borderColor = UIColor.colorWithHexString(hexString: "#F87706")
                
                let tag = TTGTextTag.init()
                tag.content = content
                tag.selectedContent = selectContent
                tag.style = normalStyle
                tag.selectedStyle = selectStyle
                
                self.textTagCollectionView.addTag(tag)
                if idx == self.selectIndex {
                    self.textTagCollectionView.updateTag(at: UInt(idx), selected: true)
                }
            }
            self.segmentedDataSource.titles = self.titles
            self.segmentedView.reloadData()
        } failure: { error in
            self.refreshButton.isHidden = false
        }
        
    }
    
    @objc func refreshButtonClick(_:UIButton) -> Void {
        loadData()
    }
}



